'use strict';

angular.module('afasApp')
  .controller('DayClassesCtrl', ['$scope', 'Services', '$state', '$filter', function ($scope, Services, $state, $filter) {

    $scope.dayClasses = [];
    $scope.days = [
      { Id: 0, alias:'Mon', label: 'Monday', selected: false },
      { Id: 1, alias:'Tue', label: 'Tuesday', selected: false },
      { Id: 2, alias:'Wed', label: 'Wednesday', selected: false },
      { Id: 3, alias:'Thu', label: 'Thursday', selected: false },
      { Id: 4, alias:'Fri', label: 'Friday', selected: false },
      { Id: 5, alias:'Sat', label: 'Saturday', selected: false },
      { Id: 6, alias:'Sun', label: 'Sunday', selected: false }
    ];
    $scope.dayClassNew = {
      'DayRange': []
    };
    $scope.selectedDays = [];
    $scope.errors = {};
    $scope.coursetypes = [];

    function applyRemoteData( newData ) {
      $scope.dayClasses = newData;
    }

    function loadRemoteData() {
      Services.getItems($scope.DAYCLASS_URL, {'include': 'course_type'})
      .then(function( results ) {
        applyRemoteData( results.data );
      });
    }

    loadRemoteData();

    // $scope.dayClasses = DayClasses.getDayClasses();
    Services.getItems('course_types')
    .then(function(results){
      $scope.coursetypes = results.data;
    });

    $scope.addDayClass = function () {
      $scope.errors = {};
      $scope.dayClassNew.DayRange = [];
      $scope.selectedDays = _.sortBy($scope.selectedDays, function(num){ return num; });
      angular.forEach($scope.selectedDays, function(value, key){
        console.log(value);
        console.log(key);
        $scope.dayClassNew.DayRange.push($scope.days[value].alias);
      });
      $scope.dayClassNew.DayRange = $scope.dayClassNew.DayRange.join();
      Services.add($scope.DAYCLASS_URL, $scope.dayClassNew)
      .then(function(){
        $scope.dayClassNew = {};
        $state.go( '^.list' );
      }, function(results){
        if (results.status == 400) {
          $scope.errors = results.data;
        }
      });
    };

    $scope.validate = function(DayClass) {
      var items = _.indexOf($scope.selectedDays, DayClass.Id);

      if (items > -1) {
        $scope.selectedDays = _.without($scope.selectedDays, DayClass.Id);
      } else {
        $scope.selectedDays.push(DayClass.Id);
      }
    };

    $scope.deleteDayClass = function (dayClass) {
      Services.delete($scope.DAYCLASS_URL, dayClass)
      .then(function(){
        loadRemoteData();
      });
    };

  }]).controller('DayClassCtrl', ['$scope', 'Services', '$state', '$stateParams', 'filterFilter', function ($scope, Services, $state, $stateParams, filterFilter) {

    $scope.days = [
      { Id: 0, alias:'Mon', label: 'Monday', selected: false },
      { Id: 1, alias:'Tue', label: 'Tuesday', selected: false },
      { Id: 2, alias:'Wed', label: 'Wednesday', selected: false },
      { Id: 3, alias:'Thu', label: 'Thursday', selected: false },
      { Id: 4, alias:'Fri', label: 'Friday', selected: false },
      { Id: 5, alias:'Sat', label: 'Saturday', selected: false },
      { Id: 6, alias:'Sun', label: 'Sunday', selected: false }
    ];

    $scope.currentDayClass = {};
    $scope.currentID = $stateParams.id;
    // $scope.currentDayClass =DayClasses.getDayClass($stateParams.id);

    angular.forEach($scope.currentDayClass.DayRange, function(value , key){
      console.log(key);
      $scope.days[value].selected = true;
    });

    Services.getItem($scope.DAYCLASS_URL, $stateParams.id)
    .then(function(results){
      $scope.currentCountry = results.data;
    });

    $scope.updateDayClass = function () {
      Services.update($scope.DAYCLASS_URL, $scope.currentDayClass, $scope.currentID)
      .then(function(){
        $state.go( '^.show', {id: $scope.currentID} );
      });
    };

    $scope.selectedDays = function () {
      return filterFilter($scope.days, { selected: true });
    };

    $scope.$watch('days|filter:{selected:true}', function (nv) {
      $scope.currentDayClass.DayRange = nv.map(function (day) {
        return day.id;
      });
    }, true);

  }]);